package com.itheima.sc.web.ums;

import com.itheima.sc.pojo.ums.UmsResource;
import com.itheima.sc.result.Result;
import com.itheima.sc.service.ums.UmsResourceService;
import com.itheima.sc.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("resource")
public class UmsResourceController {
    @Autowired
    private UmsResourceService umsResourceService;

    /**
     * 添加后台资源
     */
    @PostMapping("create")
    public Result create(@RequestBody UmsResource umsResource) {
        umsResourceService.create(umsResource);
        return Result.success(null);
    }

    /**
     * 根据ID删除后台资源
     */
    @PostMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        umsResourceService.delete(id);
        return Result.success(null);
    }

    /**
     * 分页模糊查询后台资源
     */
    @GetMapping("list")
    public Result list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                Integer categoryId,  String nameKeyword,  String urlKeyword) {
        PageResult pageResult = umsResourceService.list(pageNum, pageSize, categoryId, nameKeyword, urlKeyword);
        return Result.success(pageResult);
    }

    /**
     * 查询所有后台资源
     */
    @GetMapping("listAll")
    public Result listAll(){
        List<UmsResource> list = umsResourceService.listAll();
        return Result.success(list);
    }

    /**
     * 修改后台资源
     */
    @PostMapping("update/{id}")
    public Result update(@RequestBody UmsResource umsResource){
        umsResourceService.update(umsResource);
        return Result.success(null);
    }

    /**
     * 根据ID获取资源详情
     */
    @GetMapping("{id}")
    public Result getInfoById(@PathVariable Integer id){
        UmsResource umsResource = umsResourceService.getInfoById(id);
        return Result.success(umsResource);
    }

}
